package com.loong.seller.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.common.utils.TransformUtils;
import com.loong.seller.common.backend.param.ShopReportParam;
import com.loong.seller.mapper.ShopDeliveryMapper;
import com.loong.seller.model.ShopDelivery;
import com.loong.seller.mq.ShopReport;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Description:
 * @Author: hzp
 * @Date: 2020-11-9 9:40
 */
@Component
public class ShopDeliveryBusiness extends AbstractBusiness<ShopDelivery> {
    @Resource
    ShopReport shopReport;

    @Resource
    ShopDeliveryMapper shopDeliveryMapper;
    @Override
    public void save(List<ShopDelivery> models) {
        shopDeliveryMapper.insertList(models);
//        //上报mq 配送方式保存 修改也是重新保存
//        RepositoryEventDto<List<ShopDelivery>> shopRepositoryEventDto = RepositoryEventDto.buildWithCreate(models);
//        shopReport.report(shopRepositoryEventDto);
    }


//    public void deleteById(Object id) {
//        shopDeliveryMapper.deleteByPrimaryKey(id);
//        //上报mq 删除配送方式
//        RepositoryEventDto<Object> objectRepositoryEventDto = RepositoryEventDto.buildWithDelete(id);
//        shopReport.report(objectRepositoryEventDto);
//    }


    public void deleteByCondition(Long shopId) {
        Condition condition = new Condition(ShopDelivery.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        shopDeliveryMapper.deleteByCondition(condition);
    }

    @Override
    public void updateByPrimaryKeySelective(ShopDelivery model) {
        super.updateByPrimaryKeySelective(model);
        ShopReportParam shopParam = ShopReportParam.builder().id(model.getShopId()).basePrice(model.getBasePrice()).build();
        //上报mq
        RepositoryEventDto<ShopReportParam> shopRepositoryEventDto = RepositoryEventDto.buildWithUpdate(shopParam);
        shopReport.report(shopRepositoryEventDto);
    }
}
